package com.ow.dva.module.serverSettings.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ow.dva.config.annotation.DvaCache5M;
import com.ow.dva.module.account.entity.AccountInfo;
import com.ow.dva.module.account.service.AccountInfoService;
import com.ow.dva.module.account.service.AccountService;
import com.ow.dva.module.base.entity.json.RT;
import com.ow.dva.module.base.entity.param.DvaPage;
import com.ow.dva.module.base.service.impl.DvaServiceImpl;
import com.ow.dva.module.duty.entity.Duty;
import com.ow.dva.module.duty.entity.DutyRecord;
import com.ow.dva.module.duty.service.DutyRecordService;
import com.ow.dva.module.duty.service.DutyService;
import com.ow.dva.module.park.entity.JsonAccount;
import com.ow.dva.module.park.entity.Road;
import com.ow.dva.module.park.service.RoadService;
import com.ow.dva.module.serverSettings.entity.ServerSettings;
import com.ow.dva.module.serverSettings.mapper.ServerSettingsMapper;
import com.ow.dva.module.serverSettings.service.ServerSettingsService;
import com.ow.dva.util.UUIDFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import static com.ow.dva.module.serverSettings.app.AppServerSettingsController.isEffectiveDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JiangYanpeng
 * @since 2020-03-02
 * @version V1.0
 */
@Service
public class ServerSettingsServiceImpl extends DvaServiceImpl<ServerSettingsMapper, ServerSettings> implements ServerSettingsService {

    @DvaCache5M
    @Override
    public DvaPage<ServerSettings> page(DvaPage<ServerSettings> page, Map<String, Object> param) {

        return baseMapper.page(page,param);
    }

    @Resource
    private DutyService dutyService;
    @Resource
    private AccountService accountService;
    @Resource
    private AccountInfoService accountInfoService;
    @Resource
    private DutyRecordService dutyRecordService;
    @Resource
    private RoadService roadService;

    @Override
    public ServerSettings getByTerminaId(String terminaId) {
        return baseMapper.getByTerminaId(terminaId);
    }

    public RT getDutyAccount(String roadId){
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
         String now = sf.format(new Date());
        Calendar cal = Calendar.getInstance();
        Duty duties = null;
        try {
            Duty duty = new Duty();
            duty.setRoadId(roadId);
            int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if(i == 0){
                i = 7;
            }
            duty.setWeek(i);
            Date nowTime = sf.parse(now);
//            上午时间段
            Date amStartTime = sf.parse("06:00:00");
            Date amEndTime = sf.parse("12:00:00");
            if (isEffectiveDate(nowTime, amStartTime, amEndTime)) {
                duty.setTimeInterval(1);
                duties=dutyService.getByRoad(duty);
            }
//           下午时间段
            Date pmStartTime = sf.parse("12:00:00");
            Date pmEndTime = sf.parse("18:00:00");
            if (isEffectiveDate(nowTime, pmStartTime, pmEndTime)) {
                duty.setTimeInterval(2);
                duties=dutyService.getByRoad(duty);
            }
//            晚上时间段
            Date bntStartTime = sf.parse("18:00:00");
            Date bntEndTime = sf.parse("24:00:00");
            Date antStartTime = sf.parse("00:00:00");
            Date antEndTime = sf.parse("06:00:00");
            if (isEffectiveDate(nowTime, bntStartTime, bntEndTime) || isEffectiveDate(nowTime, antStartTime, antEndTime)) {
                duty.setTimeInterval(3);
                duties=dutyService.getByRoad(duty);
            }

        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        JSON json = new JSONObject();

        if (duties == null){
            return RT.error(1,"当前没有值班人!");
        }else {
//            保存值班信息
            saveDuty(duties);
//            返回值班人员信息
            JsonAccount jsonAccount = getAccount(duties.getAccountInfoId());
            return RT.ok("值班人信息",json.toJSONString(jsonAccount));
        }

    }

    public void saveDuty(Duty duties){
        try {
            //            保存值班信息
            Road road = roadService.getById(duties.getRoadId());
            DutyRecord dutyRecord = new DutyRecord();
            dutyRecord.setId(UUIDFactory.getStringId());
            dutyRecord.setAccountInfoId(duties.getAccountInfoId());
            dutyRecord.setAccountInfoName(duties.getAccountInfoName());
            dutyRecord.setRoadId(duties.getRoadId());
            dutyRecord.setRoadName(duties.getRoadName());
            dutyRecord.setParkId(road.getParkId());
            dutyRecord.setParkName(road.getParkName());
            dutyRecord.setDutyTime(new Date());
            dutyRecord.setDutyId(duties.getId());
            dutyRecord.setWeek(duties.getWeek());
            dutyRecord.setTimeInterval(duties.getTimeInterval());
            dutyRecordService.save(dutyRecord);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public JsonAccount getAccount(String accountInfoId){

        AccountInfo accountInfo = accountInfoService.getById(accountInfoId);
        JsonAccount jsonAccount = new JsonAccount();
        jsonAccount.setAccountInfoId(accountInfoId);
        jsonAccount.setName(accountInfo.getName());
        jsonAccount.setIdCard(accountInfo.getIdCard());
        jsonAccount.setCreateTime(accountInfo.getCreateTime());
        jsonAccount.setPhone(accountInfo.getPhone());
        jsonAccount.setEmail(accountInfo.getEmail());
        jsonAccount.setAddress(accountInfo.getAddress());
        jsonAccount.setSex(accountInfo.getSex());

        return jsonAccount;
    }

}
